import pygame
import Sprite_Group
import Explodes
import Musics
from Config import WIDTH,HEIGHT,BOOMMUSIC
import  Config


class Bullet(pygame.sprite.Sprite):
    def __init__(self,character):
        super().__init__()
        self.images = character.bulletskin
        self.direction = character.direction
        self.image = self.images[self.direction]
        self.rect=self.image.get_rect()
        self.speed=character.move_speed*2
        self.live=True
        self.mask = pygame.mask.from_surface(self.image)
        self.character = character

        if self.direction=='U':
            self.rect.x=character.rect.x+character.rect.width/2-self.rect.width/2
            self.rect.y=character.rect.y-self.rect.height
        elif self.direction=='D':
            self.rect.x = character.rect.x + character.rect.width / 2 - self.rect.width / 2
            self.rect.y =character.rect.y + character.rect.height
        elif self.direction=='L':
            self.rect.x = character.rect.x - self.rect.width
            self.rect.y = character.rect.y +character.rect.width/2-self.rect.height/2
        elif self.direction=='R':
            self.rect.x = character.rect.x + character.rect.width
            self.rect.y = character.rect.y + character.rect.width / 2 - self.rect.height / 2

    def move(self):
        if self.direction=='L':
            if self.rect.x-self.speed<0:self.live=False
            else :self.rect.x-=self.speed
        elif self.direction=='R':
            if self.rect.x + self.speed+self.rect.width> WIDTH:self.live=False
            else:self.rect.x += self.speed
        elif self.direction=='U':
            if self.rect.y-self.speed<0:self.live=False
            else :self.rect.y-=self.speed
        elif self.direction=='D':
            if self.rect.y + self.speed +self.rect.height > HEIGHT:self.live=False
            else:self.rect.y += self.speed

    def hitenemy(self):
        music = Musics.Music(BOOMMUSIC)
        if self.live:
            for enemy in Sprite_Group.Enemy_group:

                # 使用矩形碰撞检测
                if self.rect.colliderect(enemy.rect):
                    self.live = False
                    enemy.hp -= 1
                    if enemy.hp <= 0:
                        self.character.exp += enemy.level
                    boom = Explodes.Explode(enemy)
                    Sprite_Group.Explode_group.add(boom)
                    music.play()
                    break

    def hitplayer(self):
        music = Musics.Music(BOOMMUSIC)
        if self.live:
            for player in Sprite_Group.Player_group:
                # 使用矩形碰撞检测
                if self.rect.colliderect(player.rect):
                    self.live = False
                    player.hp -= 1
                    boom = Explodes.Explode(player)
                    Sprite_Group.Explode_group.add(boom)
                    music.play()
                    break

    def hitwall(self):
        if self.live:
            for wall in Sprite_Group.Wall_group:
                # 使用掩码进行碰撞检测
                offset_x = wall.rect.x - self.rect.x
                offset_y = wall.rect.y - self.rect.y
                # 检查两个掩码是否在给定偏移量处重叠
                if self.mask and wall.mask:  # 确保两个对象都有掩码
                    overlap = self.mask.overlap(wall.mask, (offset_x, offset_y))
                    if overlap:
                        self.live = False
                        break
    
    def update(self, *args, **kwargs):
        self.hitwall()
        self.hitenemy()
        if Config.get_pvp():self.hitplayer()
        self.move()

class PlayerBullet(Bullet):
    pass

class EnemyBullet(Bullet):
    def update(self, *args, **kwargs):
        self.hitwall()
        self.hitplayer()
        self.move()
